package com.identity.identity.dect.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class OverlayView extends View {
    private final RectF mRectF = new RectF();
    private final Path mPath = new Path();
    private Paint mPaint;
    public OverlayView(Context context) {
        super(context);
        init();
    }
    public OverlayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public OverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        this.mPaint = new Paint();
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setColor(0xFF777777);
        this.mPaint.setStrokeWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        this.mPath.reset();
        this.buildPath(this.mPath, this.getWidth(), this.getHeight());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipOutPath(this.mPath);
        } else {
            canvas.clipPath(this.mPath, Region.Op.DIFFERENCE);
        }
        canvas.restore();
        canvas.drawPath(this.mPath, this.mPaint);
    }

    protected void buildPath(@NonNull final Path path, final int viewWidth, final int viewHeight) {
        this.mRectF.set(viewWidth * 0.1f, viewHeight * 0.1f, viewWidth * 0.9f, viewHeight * 0.9f);
        path.addRect(this.mRectF, Path.Direction.CCW);
    }
    public Rect getMaskBounds() {
        final Rect rect = new Rect();
        this.mRectF.round(rect);
        return rect;
    }
}
